package Datebase;

import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;

public class tmex extends JFrame {
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new tmex("cno","mess");
    }
    public tmex(String cno,String mess){
        JFrame mex = new JFrame(mess+"修改");
        URL resource = tmex.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        mex.setIconImage(image);
        mex.setSize(800, 600);
        mex.setLocationRelativeTo(null);
        mex.setLayout(null);

        JLabel mok = new JLabel("请输入要修改的"+mess+":");
        mok.setFont(new Font("微软雅黑",Font.PLAIN,18));
        mok.setBounds(100,40,300,50);
        mex.add(mok);

        JTextField textField5 = new JTextField();
        textField5.setFont(new Font("微软雅黑",Font.PLAIN,18));
        textField5.setBounds(320,53,150,30);
        textField5.setColumns(10);
        mex.add(textField5);

        JButton mexx = new JButton("确认修改");
        mexx.setFont(new Font("微软雅黑",Font.PLAIN,18));
        mexx.setBounds(500,53,120,30);
        mex.add(mexx);

        mexx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (mess == "课程名") {
                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                    Connection connection = null;
                    try {
                        connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "HR", "root");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    try {
                        String pw1 = "";
                        String un = "";
                        Statement statement = connection.createStatement();

                        ResultSet resultSet = statement.executeQuery("select CNO From COURSE where CNO='" + cno + "'");

                        while (resultSet.next()) {
                            un = resultSet.getString(1);

                        }

                        JOptionPane.showMessageDialog(null, "修改成功！",
                                "提示", JOptionPane.INFORMATION_MESSAGE);
                        ResultSet resultSet1 = statement.executeQuery("UPDATE COURSE SET CNAME = '" + textField5.getText() + "' WHERE CNO = '" + cno + "'");
                        while (resultSet.next()) {
                            pw1 = resultSet1.getString(1);

                        }


                        connection.close();


                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }

                }
                else if (mess == "学分")
                {
                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                    Connection connection1 = null;
                    try {
                        connection1 = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "HR", "root");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    try {
                        String pw1 = "";
                        String un = "";
                        Statement statement = connection1.createStatement();

                        ResultSet resultSet = statement.executeQuery("select CNO From COURSE where CNO='" + cno + "'");

                        while (resultSet.next()) {
                            un = resultSet.getString(1);

                        }

                        JOptionPane.showMessageDialog(null, "修改成功！",
                                "提示", JOptionPane.INFORMATION_MESSAGE);
                        ResultSet resultSet1 = statement.executeQuery("UPDATE COURSE SET CCREDIT = '" + textField5.getText() + "' WHERE CNO = '" + cno + "'");
                        while (resultSet.next()) {
                            pw1 = resultSet1.getString(1);

                        }


                        connection1.close();


                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }
                }
                else if(mess == "学期") {
                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                    Connection connection2 = null;
                    try {
                        connection2 = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "HR", "root");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    try {
                        String pw1 = "";
                        String un = "";
                        Statement statement = connection2.createStatement();

                        ResultSet resultSet = statement.executeQuery("select CNO From COURSE where CNO='" + cno + "'");

                        while (resultSet.next()) {
                            un = resultSet.getString(1);

                        }

                        JOptionPane.showMessageDialog(null, "修改成功！",
                                "提示", JOptionPane.INFORMATION_MESSAGE);
                        ResultSet resultSet1 = statement.executeQuery("UPDATE COURSE SET TERM = '" + textField5.getText() + "' WHERE CNO = '" + cno + "'");
                        while (resultSet.next()) {
                            pw1 = resultSet1.getString(1);

                        }

                        connection2.close();

                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }
                }
            }
        });
        mex.setVisible(true);

    }
}
